The present invention relates in general to memory management, and more particularly, to a method of memory management for memory critical systems such as Smart Cards.
Smart Cards come in many forms and have many applications. One commonly known smart card is a Java Card that can support multiple applications. In the rest of this specification, Java Cards will be used by way of example, however, the invention disclosed herein is not necessarily limited to Java Cards.
When considering Java Cards that support multiple applications, runtime-enforced mechanisms known as firewalls are used to isolate memory or data spaces accessible by an application. This basically avoids corruption of instances of currently active applications known as program contexts.
Program contexts include both persistent and non-persistent contexts. The non-persistent context includes a stack and a heap. The heap is used to store temporary objects and fields of transient objects known to those skilled in the art.
Previous implementations of a memory management unit for managing the program contexts on a Java Card have resulted in inefficient use of scarce processor and memory resources on the Java Card. For example, one implementation has objects for different program contexts arranged in a haphazard manner. Access to these objects is on an object-by-object basis. Code for managing such objects is large and often results in slower execution time. There are also implementations that have specific memory sections dedicated to stack and heap use. Such implementations have resulted in inefficient use of memory on the Java Card.
Memory management techniques used in operating systems for larger computers, such as desktop computers, are not ideally suited for implementation on a Java Card or other Smart Cards. Further, current memory management techniques do not effectively provide suitable contiguous free memory availability for memory critical devices.
According to one aspect of the invention there is provided a memory management method for a memory having a plurality of program contexts including an active program context in an active region and at least one inactive program context in an inactive region, each program context having a first and a second part wherein said first and second parts of said active program context are separated by a contiguous memory space comprising a free memory block and a common data parameter store, said method comprising:
swapping a location of said second part of said active program context with said contiguous memory space to move said second part to be adjacent said first part of said first program context;
swapping a location of a said inactive program context with said active program context and any program context therebetween; and
swapping a location of a second part of said inactive program context with said contiguous memory space, after which said inactive program context becomes currently active.
Suitably, the inactive region may include a segmentation block adjacent the active region, and wherein the method further includes a prior step of:
moving a first part of the active program context to a location adjacent the at least one inactive program context.
Preferably, the method may further include a subsequent step of:
moving a first part of the inactive program context to a boundary of the active region.
Suitably, a common data parameter store may be used for passing data from the first program context to the inactive program context.
Preferably, after the step of swapping a location of a second part of the inactive program context, the common data parameter store is adjacent the second part of the active program context.
According to another aspect of the invention there is provided smartcard comprising:
memory having:
i) an inactive region with at least one inactive program context, each program context having a first and a second part; and
ii) an active region with an active program context with a first and second part separated by a contiguous memory space comprising a free memory block and a common data parameter store, wherein in use said smartcard manages memory allocation by:
a) swapping a location of said second part of said active program context with said contiguous memory space to move said second part to be adjacent said first part of said active program context;
b) swapping a location of a said inactive program context with said active program context and any program context therebetween; and
c) swapping a location of a second part of said inactive program context with said contiguous memory space after which said inactive program context becomes currently active.
Preferably, the smartcard may manage memory allocation by a prior step of:
moving a first part of said active program context to a location adjacent said at least one inactive program context.
Suitably, the smartcard may manage memory allocation by a subsequent step of:
moving a first part of said inactive program context to a boundary of said active region.
Preferably, the first part of said active program context may be located adjacent a boundary of the active region adjacent the inactive region.
Suitably, the second part may be adjacent a second boundary of the active region.
Preferably, there may be a segmentation block in the inactive region separating the first part of currently active block and the at least one inactive program context.